package com.yyk.yykywd.mvvm.ui

import android.text.format.DateUtils
import android.view.KeyEvent
import androidx.lifecycle.Observer
import com.yyk.yykywd.*
import com.yyk.yykywd.base.BaseActivity
import com.yyk.yykywd.bean.BaseBean
import com.yyk.yykywd.mvvm.view_model.AddCustomerViewModel
import com.yyk.yykywd.util.TimeUtil
import com.yyk.yykywd.util.ToastUtil
import kotlinx.android.synthetic.main.activity_add_hospital_customer.*
import kotlinx.android.synthetic.main.rl_bottom1.*
import java.util.*

class AddHospitalCustomerActivity : BaseActivity() {
    private lateinit var viewModel: AddCustomerViewModel

    override fun getLayoutId(): Int {
        return R.layout.activity_add_hospital_customer
    }

    override fun initView() {
        viewModel = getViewModel(this, AddCustomerViewModel::class.java)

        setBackArrow {
            if (judge()) {
                backLogic(mActivity) {
                    finish()
                }
            } else {
                finish()
            }
        }
        setTitle("新增医院客户")

        tv_founder.text = "业务创建人：" + getName()
        tv_department.text = getDepartment()
    }

    override fun initData() {
        viewModel.getType()
    }

    fun judge(): Boolean {
        if (getTextString(et_hospital_name).isNotBlank()) {
            return true
        }
        if (getTextString(tv_hospital_type) != "请选择") {
            return true
        }
        if (getTextString(tv_hospital_rating) != "请选择") {
            return true
        }
        if (getTextString(tv_visit_time) != "请选择") {
            return true
        }
        if (getTextString(et_visit_name).isNotBlank()) {
            return true
        }
        if (getTextString(et_visitor_position).isNotBlank()) {
            return true
        }
        if (getTextString(et_visit_phone).isNotBlank()) {
            return true
        }
        if (getTextString(et_hospital_situation).isNotBlank()) {
            return true
        }
        return false
    }

    override fun setListener() {
        super.setListener()

        viewModel.base.observe(this, Observer<BaseBean> {
            if (it.code == 306) {
                showAlreadyAdd(mActivity, HOSPITAL)
                return@Observer
            }
            if (it.code == 200) {
                ToastUtil.showShort("新增成功")
                finish()
            }

        })

        tv_save.setOnClickListener {
            if (required()) {
                return@setOnClickListener
            }
            newCustomer(mActivity) {
                addCustomer("1")
            }

        }
        tv_add.setOnClickListener {
            if (required()) {
                return@setOnClickListener
            }
            addSeas(mActivity) {
                addCustomer("3")
            }

        }

        tv_visit_time.setOnClickListener {
            showTimePicker(mActivity,
                    showYear = true,
                    showMonth = true,
                    showDay = true,
                    showHour = false,
                    showMinute = false,
                    showSecond = false,
                    startDate = viewModel.startDate,
                    endDate = viewModel.endDate,
                    pattern = TimeUtil.YYYY_MM_DD) {
                tv_visit_time.text = it
            }
        }

        tv_hospital_type.setOnClickListener {
            val showOptionsPicker = showOptionsPicker(mActivity) { options1: Int, _: Int, _: Int ->
                tv_hospital_type.text = viewModel.hospitalType[options1]
            }
            showOptionsPicker.setSelectOptions(0, 0, 0)
            showOptionsPicker.setPicker(viewModel.hospitalType) //三级选择器
            showOptionsPicker.show()
        }

        tv_hospital_rating.setOnClickListener {
            val showOptionsPicker = showOptionsPicker(mActivity) { options1: Int, _: Int, _: Int ->
                tv_hospital_rating.text = viewModel.hospitalGrade[options1]
            }
            showOptionsPicker.setSelectOptions(0, 0, 0)
            showOptionsPicker.setPicker(viewModel.hospitalGrade) //三级选择器
            showOptionsPicker.show()
        }
    }

    private fun addCustomer(flag: String) {
        viewModel.addHospitalClient(getTextString(et_hospital_name),
                getTextString(tv_hospital_type),
                getTextString(tv_hospital_rating),
                transToTimeStamp(getTextString(tv_visit_time), TimeUtil.YYYY_MM_DD).toString(),
                getTextString(et_visit_name),
                getTextString(et_visitor_position),
                getTextString(et_visit_phone),
                getTextString(et_hospital_situation),
                flag)
    }

    private fun required(): Boolean {
        if (getTextString(et_hospital_name).isBlank()) {
            ToastUtil.showShort("请填写医院名称")
            return true
        }
        if (getTextString(tv_hospital_type) == "请选择") {
            ToastUtil.showShort("请选择医院类型")
            return true
        }
        if (getTextString(tv_hospital_rating) == "请选择") {
            ToastUtil.showShort("请选择医院评级")
            return true
        }
        if (getTextString(tv_visit_time) == "请选择") {
            ToastUtil.showShort("请选择拜访时间")
            return true
        }
        if (getTextString(et_visit_name).isBlank()) {
            ToastUtil.showShort("请填写拜访人姓名")
            return true
        }
        if (getTextString(et_visitor_position).isBlank()) {
            ToastUtil.showShort("请填写拜访人职位")
            return true
        }
        if (getTextString(et_visit_phone).isBlank()) {
            ToastUtil.showShort("请填写拜访人电话")
            return true
        }
        if (getTextString(et_hospital_situation).isBlank()) {
            ToastUtil.showShort("请填写医院情况")
            return true
        }
        return false
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (judge()) {
                backLogic(mActivity) {
                    finish()
                }
            } else {
                finish()
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

}
